package zouran.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Author：zouran
 * Date：2023/3/30  13:47
 * Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class EarthquakeInfo {
    @TableId(type = IdType.AUTO)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long earthquakeId;
    private String earthquakeName;
    private Double magnitude;
    private Double highIntensity;
    private Double longitude;
    private Double latitude;
    private Double depth;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime earthquakeTime;
    @TableField(exist = false)
    private List<IntensityLine> intensityLineList = new ArrayList<>();

    public void getIntensity(double x1, double y1, double z1, double b1, double e1, double x2, double y2, double z2, double b2, double e2) {
        double longRadius = 0, shortRadius = 0;
        double ia = x1 + y1 * magnitude - z1 * Math.log10(longRadius + b1) + e1;
        double ib = x2 + y2 * magnitude - z2 * Math.log10(shortRadius + b2) + e2;
        setHighIntensity(ia);
        int intensity = (int) ia;
        intensityLineList.clear();
        int count = 0;
        while (intensity > 5) {
            longRadius = Math.pow(10, -(intensity - x1 - e1 - y1 * magnitude) / z1) - b1;
            shortRadius = Math.pow(10, -(intensity - x2 - e2 - y2 * magnitude) / z2) - b2;
            IntensityLine intensityLine = new IntensityLine(longRadius, shortRadius, -20.0, intensity, earthquakeId);
            intensityLineList.add(intensityLine);
            intensity--;
            count++;
        }
        if (count == 0) {
            longRadius = 0;
            shortRadius = 0;
            IntensityLine intensityLine = new IntensityLine(longRadius, shortRadius, -20.0, intensity, earthquakeId);
            intensityLineList.add(intensityLine);
        }
    }

    public Double getIntensityByDistance(double x1, double y1, double z1, double b1, double e1, Double meter) {
        double kilometer = meter / 1000;
        return x1 + y1 * magnitude - z1 * Math.log10(kilometer + b1) + e1;
    }

    //筛选烈度图数据，将数据库左连接查出来的intensityLineList属性全null替换为[]
    public List<IntensityLine> getIntensityLineList() {
        if (intensityLineList.size() == 1 && intensityLineList.get(0).getIntensity() == null)
            return new ArrayList<>();
        return intensityLineList;
    }

    public void setIntensityLineList(List<IntensityLine> intensityLineList) {
        IntensityLine intensityLine = new IntensityLine(null, null, null, null, null);
        this.intensityLineList.add(intensityLine);
        this.intensityLineList = intensityLineList;
    }
}
